import * as fs from 'fs';
import * as path from 'path';

async function compareTextFiles(filePath1: string, filePath2: string) {
    try {
        // 读取文件内容
        const [content1, content2] = await Promise.all([
            fs.promises.readFile(filePath1, 'utf-8'),
            fs.promises.readFile(filePath2, 'utf-8')
        ]);

        // 分割为行数组
        const lines1 = content1.split(/\r?\n/);
        const lines2 = content2.split(/\r?\n/);

        // 找出差异行号
        const diffs: number[] = [];
        const maxLines = Math.max(lines1.length, lines2.length);

        for (let i = 0; i < maxLines; i++) {
            const line1 = lines1[i] || '';
            const line2 = lines2[i] || '';
            
            if (line1.trim() !== line2.trim()) {
                diffs.push(i + 1); // 行号从1开始
            }
        }

        // 输出结果
        if (diffs.length === 0) {
            console.log('文件内容完全一致');
        } else {
            console.log(`发现差异行号：${diffs.join(', ')}`);
        }
    } catch (error) {
        console.error('文件比较失败：', error);
    }
}

// 使用示例
compareTextFiles(
    path.join('G:\\', 'docker-compose.yaml'),
    path.join('G:\\', 'docker-compose.yaml.1742348405.bak')
);